﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSHopDongLaoDong : BusinessBase
    {
        NSQuyetDinh quyetDinh = new NSQuyetDinh();
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string TenHopDongLaoDong { get; set; }
        public string MaSo { get; set; }
        public void EditHDLD(int hdId, int maHDLD, DateTime ngayHieuLuc, DateTime ngayHetHieuLuc, string ghiChu, string tenHDLD, int qdID)
        {
            List<NSHopDongLaoDong> lst = (List<NSHopDongLaoDong>)HttpContext.Current.Session["HDLD"];
            NSHopDongLaoDong pc = lst.Where(a => a.HopDongLaoDongId.Equals(hdId)).SingleOrDefault();
            pc.NgayHieuLuc = ngayHieuLuc;
            if (!ngayHetHieuLuc.Year.Equals(1))
            {
                pc.NgayHetHieuLuc = ngayHetHieuLuc;
            }
            pc.GhiChu = ghiChu;
            pc.MaHopDongLaoDongID = maHDLD;
            pc.TenHopDongLaoDong = tenHDLD;
            //chỉ thêm mới khi quyết định > 0.
            //Đã tồn tại qdID khi =0
            if (qdID > 0)
            {
                pc.QuyetDinhID = qdID;
                pc.MaSo = quyetDinh.GetMaSo(qdID);
            }
            else
            {
                pc.MaSo = quyetDinh.GetMaSo(pc.QuyetDinhID.Value);
            }
        }
        public void DeleteHDLD(int hdId)
        {
            List<NSHopDongLaoDong> lst = (List<NSHopDongLaoDong>)HttpContext.Current.Session["HDLD"];
            NSHopDongLaoDong pc = lst.Where(a => a.HopDongLaoDongId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllHDLD(int nhanVienId)
        {
            var query = (from a in db.NSHopDongLaoDongs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSHopDongLaoDongs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateHDLD_NhanVien(int nhanVienId, List<NSHopDongLaoDong> hdldObject)
        {

            var queryHDLDByNhanVienId = (from a in db.NSHopDongLaoDongs
                                         where a.NhanVienID.Equals(nhanVienId)
                                         select a).ToList();
            if (queryHDLDByNhanVienId.Count > 0)
            {
                db.NSHopDongLaoDongs.DeleteAllOnSubmit(queryHDLDByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSHopDongLaoDong pc in hdldObject)
            {
                NSHopDongLaoDong item = new NSHopDongLaoDong();
                CopyHDLD(pc, item);
                db.NSHopDongLaoDongs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyHDLD(NSHopDongLaoDong src, NSHopDongLaoDong des)
        {
            des.TenHopDongLaoDong = src.TenHopDongLaoDong;
            des.MaHopDongLaoDongID = src.MaHopDongLaoDongID;
            if (src.NgayHetHieuLuc.HasValue)
            {
                des.NgayHetHieuLuc = src.NgayHetHieuLuc;
            }
            des.NgayHieuLuc = src.NgayHieuLuc;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.QuyetDinhID = src.QuyetDinhID;
        }
        public List<NSHopDongLaoDong> LoadHDLDByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSHopDongLaoDongs
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.NgayHetHieuLuc descending
                         select a).ToList();
            foreach (NSHopDongLaoDong item in query)
            {
                item.TenHopDongLaoDong = common.LoadCommonCodeById(item.MaHopDongLaoDongID);
                item.MaSo = item.QuyetDinhID.HasValue ? quyetDinh.GetMaSo(item.QuyetDinhID.Value) : string.Empty;
            }
            return query;

        }
        public NSHopDongLaoDong GetHDLD(int nhanVienId, int maHDLD, DateTime ngayHieuLuc, DateTime ngayHetHieuLuc, string ghiChu, string tenHDLD, int qdID)
        {
            var db = new DataClassesDataContext();
            NSHopDongLaoDong hd = new NSHopDongLaoDong();
            hd.HopDongLaoDongId = 0 - DateTime.Now.Second;
            hd.NgayHieuLuc = ngayHieuLuc;
            if (!ngayHetHieuLuc.Year.Equals(1))
            {
                hd.NgayHetHieuLuc = ngayHetHieuLuc;
            }
            hd.GhiChu = ghiChu;
            hd.MaHopDongLaoDongID = maHDLD;
            hd.TenHopDongLaoDong = tenHDLD;
            hd.NhanVienID = nhanVienId;
            hd.QuyetDinhID = qdID;
            hd.MaSo = quyetDinh.GetMaSo(qdID);
            return hd;
        }
    }
}